<?php
/**
 * @version SVN: $Id: view.html.php 1218 2010-05-29 04:28:27Z elkuku $
 * @package    EasyCreator
 * @subpackage Views
 * @author     EasyJoomla {@link http://www.easy-joomla.org Easy-Joomla.org}
 * @author     Nikolai Plath {@link http://www.nik-it.de}
 * @author     Created on 28-Sep-2009
 * @license    GNU/GPL, see JROOT/LICENSE.php
 */

//-- No direct access
defined('_JEXEC') or die('=;)');

jimport( 'joomla.application.component.view');

/**
 * HTML View class for the EasyCreator Component.
 *
 * @package EasyCreator
 * @subpackage Views
 */
class EasyCreatorViewCodeEye extends JView
{
    protected $EasyProject = null;

    /**
     * Standard display method.
     * (non-PHPdoc)
     * @see libraries/joomla/application/component/JView#display($tpl)
     */
    public function display($tpl = null)
    {
        ecrLoadHelper('pearhelpers.consolehelper');

        //-- Add javascript
        ecrScript('codeeye');

        //-- Add css
        ecrStylesheet('codeeye');

        $this->ecr_project = JRequest::getCmd('ecr_project');
        $this->task = JRequest::getCmd('task');

        $this->EasyProject = EasyProjectHelper::getProject($this->ecr_project);

        if( ! $this->EasyProject)
        {
            //--Something went wrong..
            JError::raiseWarning(100, JText::sprintf('Unable to load the project %s', $this->ecr_project));
            ecrHTML::easyFormEnd();

            return;
        }

        if( ! count($this->EasyProject->copies))
        {
            JError::raiseWarning(100, JText::_('No files found'));
            ecrHTML::easyFormEnd();
            //--Something went wrong..

            return;
        }

        if(in_array($this->task, get_class_methods($this)))
        {
            //--Execute the task
            $this->{$this->task}();

            if($this->task == 'display_snip')
            {
                //--Raw view
                parent::display($tpl);
                return;
            }
        }
        else
        {
            if($this->task) echo sprintf('UNDEFINED Task %s in view %s', $this->task, $this->_name).'<br />';
        }

        //--Draw h1 header
        ecrHTML::header(JText::_('CodeEye'), $this->EasyProject);

        //--Draw the submenu
        echo $this->displayBar();

        parent::display($tpl);

        ecrHTML::easyFormEnd();
    }// function

    /**
     * Default task.
     */
    protected function codeeye()
    {
    }//function

    protected function phpcs()
    {
        $this->setLayout('phpcs');
    }//function

    protected function phpcpd()
    {
        $this->setLayout('phpcpd');;
    }//function

    protected function stats()
    {
        ecrLoadHelper('projectmatrix');

        $this->project = $this->EasyProject;

        $this->projectMatrix = new EasyProjectMatrix($this->EasyProject);

        $this->setLayout('stats');
    }//function

    protected function stats2()
    {
//        ecrLoadHelper('projectmatrix');
//
//        $this->project = $this->EasyProject;
//
//        $this->projectMatrix = new EasyProjectMatrix($this->EasyProject);

        $this->setLayout('stats2');
    }//function

    protected function reflect()
    {
    	$this->reflection();;
    }//function

    protected function reflection()
    {
        $this->project = $this->EasyProject;

        $this->setLayout('reflection');
    }//function

    protected function phpdoc()
    {
        $this->setLayout('phpdoc');
    }//function

    protected function w3cvalidation()
    {
        $this->setLayout('w3cvalidation');
    }//function

    protected function phpunit()
    {
        /*
         * Set the base directory for tests to 'tests' under component directory
         */
        switch($this->EasyProject->type)
        {
            case 'component':
                if( ! JFile::exists(JPATH_ROOT.DS.'bootstrap.php'))
                {
                    $btn = '&nbsp;<span class="ecr_button img icon-16-add" onclick="submitbutton(\'copy_bootstrap\');">'.JText::_('Copy bootstrap.php to Joomla root').'</span>';
                    ecrHTML::displayMessage(JText::_('Bootstrap file not found').$btn, 'notice');

                    return;
                }

                $this->testsBase = 'administrator'.DS.'components'.DS.$this->EasyProject->comName.DS.'tests'.DS.'unit';

                if( ! JFolder::exists(JPATH_ROOT.DS.$this->testsBase))
                {
                    $btn = '&nbsp;<span class="ecr_button img icon-16-add" onclick="submitbutton(\'create_test_dir_unit\');">'.JText::_('Create Test directory').'</span>';
                    ecrHTML::displayMessage(JText::_('No tests defined yet').$btn, 'notice');

                    return;
                }

                $this->resultsBase = 'administrator'.DS.'components'.DS.$this->EasyProject->comName.DS.'results';
                $this->setLayout('phpunit');
                break;

            default:
                ecrHTML::displayMessage(JText::sprintf('Unit tests for %s not available yet', $this->EasyProject->type), 'error');
                break;
        }//switch
    }//function

    protected function selenium()
    {
        /*
         * Set the base directory for tests to 'tests' under component directory
         */
        switch($this->EasyProject->type)
        {
            case 'component':
//                if( ! JFile::exists(JPATH_ROOT.DS.'bootstrap.php'))
//                {
//                    $btn = '&nbsp;<span class="ecr_button img icon-16-add" onclick="submitbutton(\'copy_bootstrap\');">'.JText::_('Copy bootstrap.php to Joomla root').'</span>';
//                    ecrHTML::displayMessage(JText::_('Bootstrap file not found').$btn, 'notice');
//
//                    return;
//                }

                $this->testsBase = 'administrator'.DS.'components'.DS.$this->EasyProject->comName.DS.'tests'.DS.'system';

                if( ! JFolder::exists(JPATH_ROOT.DS.$this->testsBase))
                {
                    $btn = '&nbsp;<span class="ecr_button img icon-16-add" onclick="submitbutton(\'create_test_dir_selenium\');">'.JText::_('Create Test directory').'</span>';
                    ecrHTML::displayMessage(JText::_('No tests defined yet').$btn, 'notice');

                    return;
                }

                $this->resultsBase = 'administrator'.DS.'components'.DS.$this->EasyProject->comName.DS.'results';
                $this->setLayout('selenium');
                break;

            default:
                ecrHTML::displayMessage(JText::sprintf('Unit tests for %s not available yet', $this->EasyProject->type), 'error');
                break;
        }//switch
    }//function

    /**
     * Displays the submenu
     *
     * @param $task the actual task
     * @return string html
     */
    private function displayBar()
    {
        //--Setup debugger
        $ecr_help = JComponentHelper::getParams('com_easycreator')->get('ecr_help');

        $subtasks = array(
        array('title'=> JText::_('CodeSniffer')
        , 'description' => JText::_('DESC CODE SNIFFER')
        , 'icon' => 'eye'
        , 'task' => 'phpcs'
        )
        , array('title' => JText::_('Duplicated Code')
        , 'description' => JText::_('DESC DUPLICATED CODE')
        , 'icon' => 'eye'
        , 'task' => 'phpcpd'
        )
        , array('title' => JText::_('PHPUnit Tests')
        , 'description' => JText::_('DESC PHPUNIT TESTS')
        , 'icon' =>'eye'
        , 'task' => 'phpunit'
        )
        , array('title' => JText::_('Selenium Tests')
        , 'description' => JText::_('DESC PHPUNIT TESTS')
        , 'icon' =>'eye'
        , 'task' => 'selenium'
        )
        , array('title' => JText::_('PHPDocumentor')
        , 'description' => JText::_('DESC PHPDOCUMENTOR')
        , 'icon' => 'eye'
        , 'task' => 'phpdoc'
        )
        //        , array('title' => JText::_('W3C Validation')
        //        , 'description' => JText::_('DESC W3C VALIDATION')
        //        , 'icon' =>'eye'
        //        , 'task' => 'w3cvalidation'
        //        )
        , array('title' => JText::_('Statistics')
        , 'description' => JText::_('DESC PROJECT STATISTICS')
        , 'icon' => 'chart'
        , 'task' => 'stats'
        )
        , array('title' => JText::_('Statistics2')
        , 'description' => JText::_('DESC PROJECT STATISTICS')
        , 'icon' => 'chart'
        , 'task' => 'stats2'
        )
        , array('title' => JText::_('Reflection')
        , 'description' => JText::_('DESC CODEYE REFLECTION')
        , 'icon' => 'icon'
        , 'task' => 'reflection'
        )
        );

        $htmlDescriptionDivs = '';
        $jsVars = '';
        $jsMorphs = '';
        $jsEvents = '';
        $html = '';
        $html .= '<div id="ecr_sub_toolbar" style="margin-bottom: 1em; margin-top: 0.5em;">';

        foreach($subtasks as $sTask)
        {
            if($this->EasyProject->type != 'component'
            && $sTask['task'] == 'tables')
            {
                continue;
            }

            $selected =( $sTask['task'] == $this->task ) ? '_selected' : '';
            $html .= '<span id="btn_'.$sTask['task'].'" style="margin-left: 0.3em;" class="ecr_button'.$selected.' img icon-16-'.$sTask['icon'].'" onclick="submitbutton(\''.$sTask['task'].'\');">';
            $html .= $sTask['title'].'</span>';

            if($ecr_help == 'all'
            || $ecr_help == 'some')
            {
                $htmlDescriptionDivs .= '<div class="hidden_div ecr_description" id="desc_'.$sTask['task'].'">'.$sTask['description'].'</div>';
                $jsVars .= "var desc_".$sTask['task']." = $('desc_".$sTask['task']."');\n";

                $jsEvents .= "$('btn_".$sTask['task']."').addEvents({\n"
                . "'mouseenter': showTaskDesc.bind(desc_".$sTask['task']."),\n"
                . "'mouseleave': hideTaskDesc.bind(desc_".$sTask['task'].")\n"
                . "});\n";
            }
        }//foreach

        $html .= $htmlDescriptionDivs;
        if($ecr_help == 'all'
        || $ecr_help == 'some')
        {
            $html .= "<script type='text/javascript'>"
            ."window.addEvent('domready', function() {\n"
            ."function showTaskDesc(name) {\n"
            ."this.setStyle('display', 'block');\n"
            ."}\n"
            ."function hideTaskDesc(name) {\n"
            ."  this.setStyle('display', 'none');\n"
            ."}\n"
            . $jsVars
            . $jsEvents
            . "});\n"
            . "</script>";
        }

        $html .= '</div>';

        return $html;
    }//function

}// class
